package cn.lblbc.countdown

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import cn.lblbc.countdown.R

class MainActivity : AppCompatActivity(), SensorEventListener {
    
    private lateinit var sensorManager: SensorManager
    private lateinit var compassImage: ImageView
    private lateinit var directionText: TextView
    private var currentDegree = 0f
    
    override fun onCreate(savedInstanceState: Bundle?) {
        // 启用沉浸式模式
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // 设置沉浸式状态栏
        setupImmersiveMode()
        
        // 初始化视图
        compassImage = findViewById(R.id.compass_image)
        directionText = findViewById(R.id.direction_text)
        
        // 设置标题字体
        val title = findViewById<TextView>(R.id.title)
        
        // 初始化传感器管理器
        sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
    }
    
    private fun setupImmersiveMode() {
        // 隐藏状态栏和导航栏
        window.decorView.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN
                        or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                )
        
        // 使状态栏透明
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = android.graphics.Color.TRANSPARENT
        window.navigationBarColor = android.graphics.Color.TRANSPARENT
    }
    
    override fun onResume() {
        super.onResume()
        // 恢复沉浸式模式
        setupImmersiveMode()
        
        // 注册方向传感器监听器
        sensorManager.registerListener(
            this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_GAME
        )
    }
    
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            setupImmersiveMode()
        }
    }
    
    override fun onPause() {
        super.onPause()
        // 取消注册传感器监听器
        sensorManager.unregisterListener(this)
    }
    
    override fun onSensorChanged(event: SensorEvent?) {
        event?.let {
            val degree = it.values[0]
            
            // 创建旋转动画
            val rotateAnimation = RotateAnimation(
                currentDegree,
                -degree, // 反向旋转以匹配真实方向
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f
            )
            
            rotateAnimation.duration = 210 // 稍微增加动画时间使移动更平滑
            rotateAnimation.fillAfter = true
            
            // 开始动画
            compassImage.startAnimation(rotateAnimation)
            currentDegree = -degree
            
            // 更新方向文本
            val direction = getDirection(degree)
            directionText.text = "${String.format("%.1f", degree)}° $direction"
        }
    }
    
    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
        // 传感器精度变化处理
    }
    
    private fun getDirection(degree: Float): String {
        return when {
            degree >= 337.5 || degree < 22.5 -> "北"
            degree >= 22.5 && degree < 67.5 -> "东北"
            degree >= 67.5 && degree < 112.5 -> "东"
            degree >= 112.5 && degree < 157.5 -> "东南"
            degree >= 157.5 && degree < 202.5 -> "南"
            degree >= 202.5 && degree < 247.5 -> "西南"
            degree >= 247.5 && degree < 292.5 -> "西"
            else -> "西北"
        }
    }
}